package com.example.jetpack04;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.jetpack04.adapter.MyRecycleViewAdapter;
import com.example.jetpack04.databinding.ActivityMainBinding;
import com.example.jetpack04.model.User;
import com.example.jetpack04.viewmodel.UserViewModel;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration;
import com.yanzhenjie.recyclerview.widget.DefaultLoadMoreView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    private UserViewModel userViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        userViewModel = new UserViewModel();
        activityMainBinding.setUservm(userViewModel);

        activityMainBinding.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user = new User("daluo", 23, "SIT2 ");
                userViewModel.setUser(user);
                //修改了之后必须要通知一下页面吗？？  为什么？？
                activityMainBinding.setUservm(userViewModel);
            }
        });

        activityMainBinding.jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ScoreActivity.class);
                startActivity(intent);
            }
        });


        //todo 初始化滑动菜单的时候，必须在绑定适配器之前，不然会报错
        //为rv创建侧滑菜单 使用滑动菜单的时候不能开启滑动删除，
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(MainActivity.this);
                deleteItem.setBackground(R.drawable.delete);
                deleteItem.setHeight(50);
                deleteItem.setWidth(50);

                SwipeMenuItem toTopItem = new SwipeMenuItem(MainActivity.this);
                toTopItem.setBackground(R.drawable.totop);
                toTopItem.setHeight(50);
                toTopItem.setWidth(50);

                rightMenu.addMenuItem(deleteItem);rightMenu.addMenuItem(toTopItem);
            }
        };
        activityMainBinding.rc.setSwipeMenuCreator(swipeMenuCreator);


        activityMainBinding.rc.setOnItemMenuClickListener(new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int adapterPosition) {
                //点击了menu之后执行操作时候收先要关闭menu，
                menuBridge.closeMenu();
                //看看是左边menu还是右边menu
                int direction = menuBridge.getDirection();
                //点击的按钮的索引
                int position = menuBridge.getPosition();
                Log.d("msg", "方向："+direction+"menu的索引："+position+"点击的第几个item:"+adapterPosition);
            }
        });



        //为RecycleView设置适配器
        MyRecycleViewAdapter myRecycleViewAdapter = new MyRecycleViewAdapter();
        User user1 = new User("one", 1, "1111");
        User user2 = new User("two", 2, "2222");
        User user3 = new User("three", 3, "3333");
        User user4 = new User("four", 4, "4444");
        ArrayList<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        myRecycleViewAdapter.setUsers(list);
        activityMainBinding.rc.setAdapter(myRecycleViewAdapter);
        //让item可以滑动删除
        //activityMainBinding.rc.setItemViewSwipeEnabled(true);
        //activityMainBinding.rc.setAutoLoadMore(true);
        activityMainBinding.rc.setLayoutManager(new LinearLayoutManager(this));
        //添加列表之间的分割线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        activityMainBinding.rc.addItemDecoration(dividerItemDecoration);


        //刷新和加载更多
        //下拉刷新
        activityMainBinding.refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        //两秒之后停止刷新
                        activityMainBinding.refresh.setRefreshing(false);
                    }
                }).start();

            }
        });


        //加载更多
        activityMainBinding.rc.useDefaultLoadMore();
        activityMainBinding.rc.setLoadMoreListener(new SwipeRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                //Toast.makeText(MainActivity.this , "load more " , Toast.LENGTH_SHORT).show();
                Log.i("info" , "hello");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            activityMainBinding.rc.loadMoreFinish(false , true);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }).start();

            }
        });
        activityMainBinding.rc.loadMoreFinish(false , true);





        //最后一条记录置顶
        activityMainBinding.btTotop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //RecyclerView.Adapter adapter1 = activityMainBinding.rc.getOriginAdapter();

                //MyRecycleViewAdapter adapter = (MyRecycleViewAdapter) activityMainBinding.rc.getAdapter();
                //注意这里不是getAdapter ,应该是getOriginAdapter
                MyRecycleViewAdapter adapter = (MyRecycleViewAdapter) activityMainBinding.rc.getOriginAdapter();
                adapter.toTop(adapter.getItemCount() - 1);

                //移动到顶部
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) activityMainBinding.rc.getLayoutManager();
                linearLayoutManager.scrollToPosition(0);
            }
        });

        //在最后追加10条数据
        activityMainBinding.btLoadmore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //如果使用的是swipRecycleView，需要通过getOriginAdapter才可以得到我们自定的adapter
                MyRecycleViewAdapter adapter = (MyRecycleViewAdapter) activityMainBinding.rc.getOriginAdapter();
                ArrayList<User> arrayList = new ArrayList<>();
                User u1 = new User("five", 5, "55555");
                arrayList.add(u1);
                User u2 = new User("six", 6, "66666");
                arrayList.add(u2);
                User u3 = new User("seven", 7, "77777");
                arrayList.add(u3);
                User u4 = new User("eight", 8, "88888");
                arrayList.add(u4);
                User u5 = new User("nine", 9, "99999");
                arrayList.add(u5);
                adapter.loadMore(arrayList);

                //移动到结尾
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) activityMainBinding.rc.getLayoutManager();
                int itemCount = adapter.getItemCount();
                //这里会报错一个索引越界 不知道为啥
                linearLayoutManager.scrollToPosition(itemCount);
            }
        });

        //删除最后一条记录
        activityMainBinding.btDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyRecycleViewAdapter adapter = (MyRecycleViewAdapter) activityMainBinding.rc.getOriginAdapter();
                adapter.delete(adapter.getItemCount() - 1);
            }
        });

    }
}